package org.lq.user.web;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.lq.user.entity.Users;
import org.lq.user.service.UsersService;
import org.lq.user.service.impl.UsersServiceImpl;
import org.lq.user.util.CastUtil;

/**
 * Servlet implementation class UsersServlet
 */
@WebServlet("/UsersServlet")
@MultipartConfig
public class UsersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		UsersService usersService = new UsersServiceImpl();
		String method = request.getParameter("method");
		method = method == null ? "":method;
		String returnURL = "";
		int pageIndex = 1;
		int pageSize = 5;
		
		String name =request.getParameter("name");
		String password = request.getParameter("password");
		String gender = request.getParameter("gender");
		String birthday = request.getParameter("birthday");
		String username = request.getParameter("username");
		
		String filePath= getServletContext().getRealPath("/upload");
		System.out.println(filePath);
		
		String index= request.getParameter("index");
		if(index !=null && !"".equals(index)) {
			pageIndex = CastUtil.castInt(index);
		}
		
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("pageCount", usersService.totalPage(pageSize));
		
		switch (method) {
		case "reg":
			Part part= request.getPart("icon");
			String fileName = part.getSubmittedFileName();
			String icon= UUID.randomUUID()+fileName.substring(fileName.lastIndexOf("."));
			
			Users u = new Users();
			u.setBirthday(CastUtil.castSqlDatetoString(birthday));
			u.setGender(gender);
			u.setIcon(icon);
			u.setName(name);
			u.setUserName(username);
			u.setPassword(password);
			//文件保存
			part.write("f:/user_img/"+icon);
			System.out.println(u);
			
			if(usersService.saveUser(u) == true) {
				returnURL = "login.html";
			}
			break;
		case "login":
			
			Users login_user= usersService.login(username, password);
			if(login_user!=null) {
				request.getSession().setAttribute("user", login_user);
				List<Users> list = usersService.findPage(pageIndex, pageSize);
				request.setAttribute("userList", list);
				returnURL= "main.jsp";
			}else {
				returnURL = "login.html";
			}
			break;
		default:
			List<Users> list = usersService.findPage(pageIndex, pageSize);
			request.setAttribute("userList", list);
			returnURL = "main.jsp";
			break;
		}
		
		//response.sendRedirect(returnURL);
		request.getRequestDispatcher(returnURL).forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
